<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
namespace Admin\Controller;
class OssController extends AdminController {
	
	private $oss;//OSS操作句柄
	
    public function _initialize(){
		parent::_initialize();
		if(!is_object($this->oss)){
			$this->oss = D('Oss');
		}
    }
    
    /**
     * @todo bucket列表
     */
    public function index(){
    	$this->meta_title= 'OSS云存储bucket管理';
    	$bucket = $this->oss->getBucketLists();
    	if(!empty($bucket)){
    		//根据creat_time做倒序
    		$bucket = list_sort_by($bucket, 'creat_time','desc');
    	}
        $this->assign('_list',$bucket);
        $this->assign('bucket_num',count($bucket));
        $this->display();
    }
	
    /**
     * @todo 创建一个buccket
     */
    public function addBucket(){
    	if(IS_POST){
    		if($this->oss->addBucket()){
    			$this->success('操作成功',U('Oss/index'));
    		}else{
    			$this->error($this->oss->getError());
    		}
    	}else{
    		if(10 <= count($this->oss->getBucketLists())){
    			$this->error('阿里oss最多创建10个bucket~');
    		}
    		$this->meta_title= '添加bucket';
    		$this->display('addBucket');
    	}
    }
    
    /**
     * @todo 删除一个bucket
     */
    public function delBucket(){
    	if(IS_AJAX){
    		if($this->oss->delBucket()){
    			$this->success('操作成功');
    		}else{
    			$this->error($this->oss->getError());
    		}
    	}else{
    		$this->error('帅哥.~你想跳墙?换一家试试吧~');
    	}
    }
    
    /**
     * @todo bucket权限设置
     */
    public function bucketAcl(){
    	$this->meta_title= 'bucket权限设置';
    	$obj = D('Oss');
    	$bucket_list = $obj->getBucketLists();
    	if(empty($bucket_list)){
    		$this->error('请不要非法访问');
    	}
    	$bucket_name = array_column($bucket_list, 'name');
    	$bucket = $obj->checkBucket();
    	if($bucket == '' || !in_array($bucket, $bucket_name)){
    		$this->error('请求参数有误');
    	}
    	//获取权限list,循环.
    	$my_acl = $obj->getBucketAcl($bucket);
    	if(IS_POST && IS_AJAX){
    		$acl = trim(I('post.acl'));
    		if(!in_array($acl, array_keys(C('OSS_ACL_CONFIG')))){
    			$this->error('ACL参数不正确~');
    		}
    		if($this->oss->setBucketAcl(array('Bucket'=>$bucket,'ACL'=>$acl))){
    			$this->success('操作成功');
    		}else{
    			$this->error($this->oss->getError());
    		}
    	}else{
    		$this->assign('acl_list',C('OSS_ACL_CONFIG'));
    		//获取我的权限.做选中
    		$this->assign('my_acl',$my_acl);
    		$this->display('bucketAcl');
    	}
    }
    
    
    /**
     * @todo Bucket下级Obejct列表
     */
    public function objectLists(){
    	if(IS_AJAX){
    		$object_list = $this->oss->getObjectListByBucket();
    		if(!$object_list){
    			$this->error($this->oss->getError());
    		}
    		foreach($object_list['list'] as $key=>$val){
    			if($val['last_modify_time'] !=''){
    				$object_list['list'][$key]['last_modify_time'] = time_format($val['last_modify_time']);
    			}else{
    				$object_list['list'][$key]['last_modify_time'] = '-';
    			}
    			if($val['prefix']!=''){
    				$object_list['list'][$key]['en_prefix']=ossEncrypt($val['prefix']);
    			}
    		}
    		$json['status']	=	1;
    		$json['info']	=	'加载成功!';
    		$json['data']	=	$object_list;
    		$this->ajaxReturn($json);
    	}else{
	    	$this->meta_title= '文件列表';
	    /* 	$this->assign('_list',$object_list['list']);
	    	$this->assign('is_truncated',$object_list['is_truncated']);
	    	$this->assign('next_marker',$object_list['next_marker']); */
	    	$this->display('objectLists');
    	}
    }
    
   /**
    * @todo 删除文件夹和文件
    */
    public function del(){
    	if(IS_AJAX && IS_POST){
    		if($this->oss->deleteObject()){
    			$this->success('操作成功');
    		}else{
    			$this->error($this->oss->getError());
    		}
    	}else{
    		$this->error('小宝贝你跟谁俩呐?');
    	}
    }
    
    /**
     * @todo 创建目录
     */
    public function creatDir(){
    	if(IS_POST){
    		if($this->oss->creatDir()){
    			$this->success('操作成功!',U('objectLists',array('bucket'=>I('bucket'))));
    		}else{
    			$this->error($this->oss->getError());
    		}
    	}else{
    		$bucket = $this->oss->checkBucket();
    		if(!$bucket){
    			$this->error('bucket参数有误');
    		}
    		$this->meta_title= '创建文件夹';
    		$this->assign('bucket',$bucket);
    		$this->display('creatDir');
    	}
    }
    
    /**
     * @todo 上传文件
     */
    public function creatFile(){
    	if(IS_POST){
    		if(!empty($_FILES['file'])){
    			if($file_name = $this->oss->creatFile()){
    				$array =array('status'=>1,'new_name'=>$file_name,'old_name'=>$_FILES['file']["name"]);
    				$this->ajaxReturn($array);
//     				exit('{"jsonrpc" : "2.0", "result" : null, "id" : "id","info":"'.$file_name.'"}');
    			}else{
    				$this->error($this->oss->getError());
    			}
    		}else {
    			$this->error('请选择文件;不要尝试入侵哦~');
    		}
    	}else{
    		$this->meta_title= '上传文件';
    		$bucket = $this->oss->checkBucket();
    		if(!$bucket){
    			$this->error('bucket参数有误');
    		}
    		$this->display('creatFile');
    	}
    }

}
